package com.tqjc.work.step;

import com.tqjc.entity.Task;
import com.tqjc.entity.TaskApart;
import com.tqjc.entity.WorkStatus;
import com.tqjc.exception.TaskRunningException;
import com.tqjc.system.core.util.SpringUtils;
import com.tqjc.utils.PlcOperator;
import com.tqjc.work.TaskCallback;
import com.tqjc.work.TaskStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;



@Slf4j
@Component
@Scope("prototype")
public class MoveCraneStep extends TaskApart implements TaskCallback {
    @Resource
    PlcOperator plcOperator;

    @Resource
    TaskStatus taskStatus;

    public MoveCraneStep() {
        this.setType("move");
    }

    @Override
    public void onPreExecute() {
        try {
            log.info("开始执行步骤前置，{}", getType());

            WorkStatus status = taskStatus.getWorkStatus4Crane();
            status.setNowStep(getType());
            taskStatus.setWorkStatus4Crane(status);

            setBeginTime(new Date());
            log.info("步骤前置执行结束，{}", getType());
        } catch (Exception e) {
            log.error(getType()+"前置出现异常",e);
            e.printStackTrace();
        }
    }

    @Override
    public void onPostExecute() {
        try {
            log.info("开始执行步骤后置，{}", getType());
            setEndTime(new Date());
            //SpringUtils.getBean(TaskApartServiceImpl.class).insert(this);

            //修改步骤状态
            WorkStatus status = taskStatus.getWorkStatus4Crane();
            status.setNowStep("");
            taskStatus.setWorkStatus4Crane(status);
            log.info("步骤后置执行结束，{}", getType());

            //清除bean
            destroy();
        } catch (Exception e) {
            log.error(getType()+"后置出现异常",e);
            e.printStackTrace();
        }
    }
    @Override
    public void setTaskApartByTask(Task task) {
        this.setCallback(this);
        this.setTaskId(task.getId());
        this.setBeginX(task.getBeginX());
        this.setBeginY(task.getBeginY());
        this.setBeginZ(task.getBeginZ());
        this.setEndX(task.getEndX());
        this.setEndY(task.getEndY());
        this.setEndZ(task.getEndZ());
    }

    @Override
    public void customAction() throws TaskRunningException {
        try {
            //写入任务坐标
            log.info("步骤{}写入PLC开始", getType());
            //plcOperator.writeValueByLabelAndCraneType(WorkConst.TARGET_CRANE_X, getEndX().toString());
            //plcOperator.writeValueByLabelAndCraneType(WorkConst.TARGET_CRANE_Y, getEndY().toString());
            //plcOperator.writeValueByLabelAndCraneType(WorkConst.TARGET_CRANE_Z, getEndZ().toString());

            //写入任务类型
//            TODO:等待确认任务类型
            //plcOperator.writeValueByLabelAndCraneType(WorkConst.STEP_TYPE, "1", craneType);

            //写入任务开始
            //plcOperator.writeValueByLabelAndCraneType(WorkConst.STEP_START, "1", craneType);
            log.info("步骤{}写入PLC完成,等待任务完成", getType());
            boolean isFinished = false;
            while (!isFinished) {
                //if (Boolean.parseBoolean(plcOperator.readValueByLabelAndCraneType(WorkConst.STEP_FINISH, craneType))) {
                if(true){
                    isFinished = true;
                    log.info("PLC反馈步骤{}完成", getType());
                }
                //TODO:测试使用
                Thread.sleep(5000);
                isFinished = true;
                Thread.sleep(500);
            }

        } catch (Exception e) {
            throw new TaskRunningException(e.getMessage());
        }


    }


    @Override
    public void destroy() throws Exception {

    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }


}
